package com.wei.czz.common.enums.annex;

import com.wei.czz.common.dto.common.NameValue;
import com.wei.czz.common.tool.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.wei.czz.common.enums.annex.AnnexFieldCompareModeEnum.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2024-01-18 16:29:55
 * className: AnnexConditionEnum
 * version: 1.0
 * description:
 */
@Getter
@AllArgsConstructor
public enum AnnexConditionEnum {

    PROVINCE(1, "所属省份", "province", Arrays.asList(CONTAIN_NUMBER, NOT_CONTAIN_NUMBER)),
    CITY(2, "所属城市", "city", Arrays.asList(CONTAIN_NUMBER, NOT_CONTAIN_NUMBER)),
    DISTRICT(3, "所属区县", "district", Arrays.asList(CONTAIN_NUMBER, NOT_CONTAIN_NUMBER)),
    STREET(4, "街道", "street", Arrays.asList(CONTAIN_NUMBER, NOT_CONTAIN_NUMBER)),

    STORE_NAME(50, "商店名称", "storeName",
            Arrays.asList(EQUALS_STRING, NOT_EQUALS_STRING)
    ),
    STORE_MAIN_CLASS(51, "商店经营大类", "mainClass",
            Arrays.asList(CONTAIN_NUMBER, NOT_CONTAIN_NUMBER)
    ),
    STORE_SUB_CLASS(52, "商店经营小类", "subClass",
            Arrays.asList(CONTAIN_NUMBER, NOT_CONTAIN_NUMBER)
    ),



    ;

    /**
     * 值
     */
    private final Integer value;

    /**
     * 名称
     */
    private final String name;

    /**
     * 字段名
     */
    private final String field;

    /**
     * 比较方式枚举列表
     */
    private final List<AnnexFieldCompareModeEnum> compareModeList;

    /**
     * 获取枚举对象
     * @param value 值
     * @return 枚举对象
     */
    public static AnnexConditionEnum get(Integer value) {
        for (AnnexConditionEnum annexConditionEnum : values()) {
            if (annexConditionEnum.value.equals(value)) {
                return annexConditionEnum;
            }
        }
        return null;
    }

    /**
     * 获取名称
     * @param value 值
     * @return 名称
     */
    public static String getName(Integer value) {
        for (AnnexConditionEnum annexConditionEnum : values()) {
            if (annexConditionEnum.value.equals(value)) {
                return annexConditionEnum.name;
            }
        }
        return "";
    }

    /**
     * 获取枚举列表
     */
    public static List<NameValue<AnnexConditionDto>> getList() {
        return Stream.of(values())
                .map(annexConditionEnum ->
                        new NameValue<>(annexConditionEnum.name, AnnexConditionDto.map(annexConditionEnum))
                )
                .collect(Collectors.toList());
    }

    @Getter
    @Setter
    public static class AnnexConditionDto extends BaseEntity {

        /**
         * 值
         */
        private Integer value;

        /**
         * 名称
         */
        private String name;

        /**
         * 字段名
         */
        private String field;

        /**
         * 比较方式枚举列表
         */
        private List<NameValue<Integer>> compareModeList;

        /**
         * 枚举对象映射
         * @param annexConditionEnum 枚举对象
         * @return 映射结果
         */
        public static AnnexConditionDto map(AnnexConditionEnum annexConditionEnum) {
            AnnexConditionDto annexConditionDto = new AnnexConditionDto();
            annexConditionDto.setValue(annexConditionEnum.value);
            annexConditionDto.setName(annexConditionEnum.name);
            annexConditionDto.setField(annexConditionEnum.field);
            annexConditionDto.setCompareModeList(
                    annexConditionEnum.compareModeList.stream()
                            .map(annexFieldCompareModeEnum -> new NameValue<>(annexFieldCompareModeEnum.getName(),
                                    annexFieldCompareModeEnum.getValue())
                            )
                            .collect(Collectors.toList())
            );
            return annexConditionDto;
        }
    }
}
